using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExpertSMS.Models;

namespace ExpertSMS.Controllers
{   
    public class StoreProductStocksController : Controller
    {
        private ExpertSMSContext context = new ExpertSMSContext();

        //
        // GET: /StoreProductStocks/

        public ViewResult Index(int storeID)
        {
            Store store = context.Stores.Single(x => x.StoreID == storeID);
            ViewBag.Store = store;
            var s = from x in context.StoreProductStocks.Where(x => x.StoreID == storeID)
                    select x;
            var p = from x in context.StoreProductStocks.Where(x => x.StoreID == storeID)
                    select new
                    {
                        id = x.ProductID,
                        name = x.Product.Name,
                        unit = x.Product.Units.FirstOrDefault(y=>!y.ParentUnitID.HasValue).Name
                    };
            List<_StoreProductStockGroup> list = new List<_StoreProductStockGroup>();
            foreach (var item in p.Distinct())
            {
                list.Add(new _StoreProductStockGroup
                {
                    ID=item.id,
                    Name = item.name,
                    Unit = item.unit,
                    Quantity = s.Where(x => x.ProductID == item.id).Sum(x => x.CurrentQuantity)
                });
            }
            
            return View(list);
        }
        

        //
        // GET: /StoreProductStocks/Details/5

        public ViewResult Details(int id)
        {
            StoreProductStock storeproductstock = context.StoreProductStocks.Single(x => x.StoreProductStockID == id);
            return View(storeproductstock);
        }

        //
        // GET: /StoreProductStocks/Create

        public ActionResult Create()
        {
            ViewBag.PossibleStores = context.Stores;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View();
        }

        
        
        [HttpPost]
        public ActionResult Create(StoreProductStock storeproductstock)
        {
            if (ModelState.IsValid)
            {
                context.StoreProductStocks.Add(storeproductstock);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.PossibleStores = context.Stores;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(storeproductstock);
        }
        
        //
        // GET: /StoreProductStocks/Edit/5
 
        public ActionResult Edit(int id)
        {
            StoreProductStock storeproductstock = context.StoreProductStocks.Single(x => x.StoreProductStockID == id);
            ViewBag.PossibleStores = context.Stores;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(storeproductstock);
        }

        //
        // POST: /StoreProductStocks/Edit/5

        [HttpPost]
        public ActionResult Edit(StoreProductStock storeproductstock)
        {
            if (ModelState.IsValid)
            {
                context.Entry(storeproductstock).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleStores = context.Stores;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(storeproductstock);
        }

        //
        // GET: /StoreProductStocks/Delete/5
 
        public ActionResult Delete(int id)
        {
            StoreProductStock storeproductstock = context.StoreProductStocks.Single(x => x.StoreProductStockID == id);
            return View(storeproductstock);
        }

        //
        // POST: /StoreProductStocks/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            StoreProductStock storeproductstock = context.StoreProductStocks.Single(x => x.StoreProductStockID == id);
            context.StoreProductStocks.Remove(storeproductstock);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}